﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Willow.Extensions;
using Willow.IService.Sys;
using Willow.Model;
using Willow.MVC.Result;
using Willow.MVC.Search;
namespace Willow.WebApplication.Areas.Sys.Controllers
{
    public class DictionaryController : Controller
    {

        public IDictionaryItemService DictionaryItemService { get; set; }
        public IDictionaryTypeService DictionaryTypeService { get; set; }

        public DictionaryController(IDictionaryItemService dictionaryItemService, IDictionaryTypeService dictionaryTypeService)
        {
            DictionaryItemService = dictionaryItemService;
            DictionaryTypeService = dictionaryTypeService;
        }

        #region 页面

        /// <summary>
        /// 字典管理
        /// </summary>
        /// <returns></returns>
        public ViewResult Management()
        {
            return View();
        }

        #endregion

        #region 操作

        /// <summary>
        /// 保存字典类型
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns></returns>
        public JsonResult SaveType(Sys_DictionaryType obj)
        {
            return this.StandardJson(() =>
            {
                DictionaryTypeService.Save(obj);
            });
        }

        /// <summary>
        /// 为树获取全部字典类型
        /// </summary>
        /// <returns></returns>
        public JsonResult GetAllTypeForTree()
        {
            var data = DictionaryTypeService.Filter(p => p.IsDelete == false).Select(p => new
             {
                 id = p.Id,
                 text = p.Name,
                 iconCls = "icon-bullet_red",
                 attributes = new
                 {
                     remark = p.Remark
                 }
             });
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 删除字典类型
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public JsonResult DeleteType(Guid id)
        {
            return this.StandardJson(() =>
            {
                DictionaryTypeService.LogicalDelete(id);
            });
        }

        /// <summary>
        /// 获取分页后的字典项信息
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="rows">The rows.</param>
        /// <param name="sort">The sort.</param>
        /// <param name="order">The order.</param>
        /// <param name="typeId">The type identifier.</param>
        /// <param name="query">The query.</param>
        /// <returns></returns>
        public JsonResult GetPagedDictionaryItem(int page, int rows, string sort, string order, Guid typeId, QueryModel query)
        {
            int total = 0;
            var filter = DictionaryItemService.Query(p => p.IsDelete == false && p.TypeId == typeId);
            var lstData = filter
                .OrderBy(new PropertySortCondition(sort, order))
                .Where(query, page, rows, out total)
                .Select(p => new
                {
                    p.Id,
                    p.Name,
                    p.TypeId,
                    p.Remark,
                    p.DisplayOrder,
                    TypeName = p.Sys_DictionaryType.Name
                }).ToList();

            return this.JsonNet(new { rows = lstData, total });
        }

        /// <summary>
        /// 提交字典项
        /// </summary>
        /// <param name="lstInsert">The LST insert.</param>
        /// <param name="lstDelete">The LST delete.</param>
        /// <param name="lstUpdate">The LST update.</param>
        /// <returns></returns>
        public JsonResult SubmitItem(List<Sys_DictionaryItem> lstInsert, List<Sys_DictionaryItem> lstDelete, List<Sys_DictionaryItem> lstUpdate)
        {
            return this.StandardJson(() =>
            {
                DictionaryItemService.Submit(lstInsert, lstDelete, lstUpdate);
            });
        }

        #endregion


    }
}